﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using PrVyd.Zakladni_tridy;
using System.Xml.Linq;

namespace PrVyd.Dialogova_okna
{
    partial class PridejPlatbu : Form
    {
        public Payment DialogPayment {get; set;}
        
        public PridejPlatbu()
        {
            InitializeComponent();
            DialogPayment = new Payment();

            this.txtSenderName.Text = Zakladni_tridy.ConfigManager.Name + " " + Zakladni_tridy.ConfigManager.Surname;
            this.txtSenderDIC.Text = Zakladni_tridy.ConfigManager.DIC;
            this.txtSenderIC.Text = Zakladni_tridy.ConfigManager.ICO;
            this.txtBankNumberSender.Text = Zakladni_tridy.ConfigManager.Bill;
            this.txtBankCodeSender.Text = Zakladni_tridy.ConfigManager.BankCode;
        }

        private void PridatButton_Click(object sender, EventArgs e)
        {
            Payment tempPayment = new Payment();

            Int32 number;
            if (!Int32.TryParse(txtNumber.Text, out number))
            {
                MessageBox.Show("Chybné číslo platby", "Chyba", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.DialogResult = DialogResult.None;
                return;
            }

            if (txtNumber.Enabled)
            {
                XDocument xDoc = DatabaseManager.LoadDBX();
                if ((from x in xDoc.Descendants("payment")
                     where x.Element("number").Value == number.ToString()
                     select x).Count() > 0)
                {
                    MessageBox.Show("Platba s tímto číslem již existuje.", "Chyba", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.DialogResult = DialogResult.None;
                    return;
                }
            }
            tempPayment.Number = number;

            if (txtVS.Text.Length == 0) tempPayment.VS = "";
            else tempPayment.VS = txtVS.Text;

            Decimal amount;
            if (!Decimal.TryParse(txtAmount.Text.Replace('.', ','), out amount))
            {
                MessageBox.Show("Chybná částka platby", "Chyba", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.DialogResult = DialogResult.None;
                return;
            }
            tempPayment.Amount = amount;

            tempPayment.Date = dtpDate.Value;

            if (txtDescription.Text.Length == 0) tempPayment.Description = "";
            else tempPayment.Description = txtDescription.Text;

            if (txtSenderName.Text.Length == 0)
            {
                MessageBox.Show("Jméno odesílatele musí být vyplněno", "Chyba", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.DialogResult = DialogResult.None;
                return;
            }

            if (txtSenderIC.Text.Length > 0 && !Regex.IsMatch(txtSenderIC.Text, @"^[A-Z]{0,2}[0-9]+$"))
            {
                MessageBox.Show("IČ odesílatele musí být vyplněno správně", "Chyba", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.DialogResult = DialogResult.None;
                return;
            }

            if (txtSenderDIC.Text.Length > 0 && !Regex.IsMatch(txtSenderDIC.Text, @"^([A-Z]{2}[0-9]+)?$"))
            {
                MessageBox.Show("DIČ odesílatele musí být vyplněno správně", "Chyba", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.DialogResult = DialogResult.None;
                return;
            }

            tempPayment.Sender = new Zakladni_tridy.Person() { Firm = txtSenderName.Text, Dic = txtSenderDIC.Text, Ic = txtSenderIC.Text};

            if (txtRecipientName.Text.Length == 0)
            {
                MessageBox.Show("Jméno příjemce musí být vyplněno", "Chyba", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.DialogResult = DialogResult.None;
                return;
            }

            if (txtRecipientIC.Text.Length > 0 && !Regex.IsMatch(txtRecipientIC.Text, @"^[A-Z]{0,2}[0-9]+$"))
            {
                MessageBox.Show("IČ příjemce musí být vyplněno správně", "Chyba", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.DialogResult = DialogResult.None;
                return;
            }

            if (txtRecipientDIC.Text.Length > 0 && !Regex.IsMatch(txtRecipientDIC.Text, @"^([A-Z]{2}[0-9]+)?$"))
            {
                MessageBox.Show("DIČ příjemce musí být vyplněno správně", "Chyba", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.DialogResult = DialogResult.None;
                return;
            }
            tempPayment.Recipient = new Zakladni_tridy.Person() { Firm = txtRecipientName.Text, Dic = txtRecipientDIC.Text, Ic = txtRecipientIC.Text };


            if ((txtBankNumberRecipient.Text.Length == 0) || (txtBankCodeRecipient.Text.Length == 0))
            {
                MessageBox.Show("Bankovní účet příjemce musí být vyplněn správně", "Chyba", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.DialogResult = DialogResult.None;
                return;
            }
            tempPayment.BankAccountOfRecipient = new Zakladni_tridy.BankAccount() { AccountNumber = txtBankNumberRecipient.Text, BankCode = txtBankCodeRecipient.Text, SWIFT = txtSwiftRecipient.Text };


            if ((txtBankNumberSender.Text.Length == 0) || (txtBankCodeSender.Text.Length == 0))
            {
                MessageBox.Show("Bankovní účet odesílatele musí být vyplněn správně", "Chyba", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.DialogResult = DialogResult.None;
                return;
            }
            tempPayment.BankAccountOfSender = new Zakladni_tridy.BankAccount() { AccountNumber = txtBankNumberSender.Text, BankCode = txtBankCodeSender.Text, SWIFT = txtSwiftSender.Text };

            this.DialogPayment = tempPayment;
            this.DialogResult = DialogResult.OK;
        }

        public void FillWithPayment(Payment payment)
        {
            txtNumber.Text = payment.Number.ToString();
            txtNumber.Enabled = false;
            txtAmount.Text = payment.Amount.ToString();
            txtVS.Text = payment.VS.ToString();
            txtSenderName.Text = payment.Sender.Firm;
            txtSenderIC.Text = payment.Sender.Ic;
            txtSenderDIC.Text = payment.Sender.Dic;
            txtRecipientName.Text = payment.Recipient.Firm;
            txtRecipientIC.Text = payment.Recipient.Ic;
            txtRecipientDIC.Text = payment.Recipient.Dic;
            txtBankNumberSender.Text = payment.BankAccountOfSender.AccountNumber;
            txtBankCodeSender.Text = payment.BankAccountOfSender.BankCode;
            txtSwiftSender.Text = payment.BankAccountOfSender.SWIFT;
            txtBankNumberRecipient.Text = payment.BankAccountOfRecipient.AccountNumber;
            txtBankCodeRecipient.Text = payment.BankAccountOfRecipient.BankCode;
            txtSwiftRecipient.Text = payment.BankAccountOfRecipient.SWIFT;
            txtDescription.Text = payment.Description;
            dtpDate.Value = payment.Date;
                
        }
    }
}
